; (function (window, $, jquery) {
    $(function() {
        setTimeout(function () {
            $("#loginTab").css("color","#d4b36b");
            $(".navbar .container").css("border-bottom","1px solid #393838")
        },100)
        $(".login").height($(window).height());
        $(".login").css('min-height','720px');
        $(window).resize(function () {          //当浏览器大小变化时
            $(".login").height($(window).height());
        });
        var phoneInputFlag = false,
            verificationInoutFlag = false,
            passwordInoutFlag = false;
        var countdown = 60,
            timeOutId,
            sessionId,
            tempCheckNo;

        //清空手机号
        clearPho = function () {
            $("#loginNo").val("");
            phoneInputFlag = false;
            $('.sj').addClass('errb');
            // $('.dl').removeClass('active');
            $('.megTime').removeClass('active');
        }

        //改变密码输入框状态
        $("#hidepwd").on("click",function () {
            if($("#loginPwd").attr("type") == "password"){
                $("#loginPwd").attr("type","text");
                $(this).attr("src","https://winhc.oss-cn-shanghai.aliyuncs.com/webImg/showKey.png")
            }else{
                $("#loginPwd").attr("type","password");
                $(this).attr("src","https://winhc.oss-cn-shanghai.aliyuncs.com/webImg/hideKey.png")
            }
        })


        // 手机号监听
        $('#loginNo').on('input propertychange', function () {
            $(".loginerror").removeClass("show").find('span').html('')//移除错误提示
            var phoneValue = $(this).val();
            if (/^1\d{10}$/.test(phoneValue)) {
                phoneInputFlag = true;
                $('.sj').removeClass('errb');
                $('.megTime').addClass('active');
                // if (passwordInoutFlag) {
                //     $('.dl').addClass('active');
                // }
            } else {
                phoneInputFlag = false;
                $('.sj').addClass('errb');
                // $('.dl').removeClass('active');
                $('.megTime').removeClass('active');
            }
        });

        //密码监听
        $('#loginPwd').on('input propertychange', function () {
            $(".loginerror").removeClass("show")//移除错误提示
            var pwdValue = $(this).val();
            if (/^.+\S$/.test(pwdValue)) {
                passwordInoutFlag = true;
                $('.pwd').removeClass('errb');
                // if (phoneInputFlag) {
                    // $('.dl').addClass('active');
                // }
            } else {
                passwordInoutFlag = false;
                $('.pwd').addClass('errb');
                // $('.dl').removeClass('active');
            }
        });
        
        // setTimeout(function () {
            // $('#loginPwd').val("")
            // var phoneValueSave = $('#loginNo').val(),
            //     pwdValueSave = $('#loginPwd').val();
            // if(/^1(3|4|5|7|8)\d{9}$/.test(phoneValueSave) && /^.+\S$/.test(pwdValueSave)) {
            //     alert('1')
            //     passwordInoutFlag = true;
            //     phoneInputFlag = true;
            //     $('.dl').addClass('active');
            // }
        // },800);

        


        //验证码监听
        $('#mobilecheckNo').on('input propertychange', function () {
            $(".loginerror").removeClass("show").find('span').html('')//移除错误提示
            if (/^\d{6}$/.test($(this).val())) {
                verificationInoutFlag = true;
                $('.yzm').removeClass('errb');
                // if (phoneInputFlag) {
                //     $('.dl').addClass('active');
                // }
            } else {
                verificationInoutFlag = false;
                $('.yzm').addClass('errb');
                // $('.dl').removeClass('active');
            }
        });

        //点击验证码登录
        $(".wm").on("click",function () {
            $(".loginerror").removeClass("show").find('span').html('')
            // $('.dl').removeClass('active');//登录按钮置否
            if($(this).find("label").text() == "验证码登录"){
                $("#mobilecheckNo").val("");//清空验证码
                verificationInoutFlag = false;

                $(".pwd").addClass("hide");
                $(".yzm").addClass("show");
                $(this).find("label").text("密码登录");
                if(phoneInputFlag){
                    $(".megTime").addClass("active");
                }
            }else{
                $("#loginPwd").val("");//清空密码
                passwordInoutFlag = false;

                $(".yzm").removeClass("show");
                $(".pwd").removeClass("hide");
                $(this).find("label").text("验证码登录");
                // if (passwordInoutFlag) {
                //     $('.dl').addClass('active');
                // }
            }
        });
        toggleLoginWay = function() {
            $(".loginerror").removeClass("show").find('span').html('')

            if($('.wm').find("label").text() == "验证码登录"){
                $("#mobilecheckNo").val("");//清空验证码
                verificationInoutFlag = false;

                $(".pwd").addClass("hide");
                $(".yzm").addClass("show");
                $('.wm').find("label").text("密码登录");
                if(phoneInputFlag){
                    $(".megTime").addClass("active");
                }
            }else{
                $("#loginPwd").val("");//清空密码
                passwordInoutFlag = false;

                $(".yzm").removeClass("show");
                $(".pwd").removeClass("hide");
                $('.wm').find("label").text("验证码登录");
            }
        }
        
        //点击获取验证码
        $(".megTime").on("click",function () {
            //开始倒计时
            $(this).removeClass("active");
            settime($(this));

            var headerInfo = {},bizInfo = {};
            headerInfo.pageNum = '';
            headerInfo.pageSize = '';
            headerInfo.sessionId = "";

            bizInfo.smsKind = "checkNo";
            bizInfo.mobileNo = $("#loginNo").val();
            var serviceName = "sendCheckNo";
            $.ajax({
                url:'/biz.ajax',
                method:'get',
                cache:false,
                headers: {
                    "Accept" : "text/plain; charset=utf-8",
                    "Content-Type": "text/plain; charset=utf-8"
                },
                data:{
                    'serviceName': serviceName,
                    'headerInfo': JSON.stringify(headerInfo),
                    'bizInfo': JSON.stringify(bizInfo)
                },
                success:function(data,textStatus,jqXHR){
                    var returnData = JSON.parse(data);
                    tempCheckNo = returnData.body;
                },
                error:function(xhr,textStatus){
                    console.log('失败=',xhr)
                }
            })
        });

        //发送验证码倒计时
        function settime(obj) {
            if (countdown == 0) {
                obj.addClass("active");
                obj.text("重新获取");
                countdown = 60;
                return;
            } else {
                obj.text( countdown + "s");
                countdown--;
            }
            timeOutId = setTimeout(function() {
                    settime(obj) }
                ,1000)
        }


        //点击登录
        loginUser = function () {
            // keyLogin();
            if($(".wm").find("label").text() == "验证码登录"){
                pwdLogin();
            }else{
                yzmLogin();
            }
        }
        document.onkeydown=function(event){
            var e = event || window.event || arguments.callee.caller.arguments[0];
            if(e && e.keyCode==13){ // enter 键
//要做的事情
                loginUser()
            }
        };
        //密码登录
        function pwdLogin() {
            var username = $("#loginNo").val();
            var pwd = $("#loginPwd").val();
            var serviceName = "userLogin";

            var bizInfo = {};
            bizInfo.mobileNo =  username;
            bizInfo.loginType = "password";
            bizInfo.password =  pwd;
            $.ajax({ 
                url:'/doLogin',
                type:'post',
                data: bizInfo,
                success: function(data,status){ 
                    console.log("status====",status)
                    console.log("data====",data)
                    if(status == 'success'){ 
                        location.href = '/user';
                    }
                },
                error: function(data,status){ 
                    console.log("statusError====",status)
                    console.log("dataError====",data)
                    if(status == 'error'){ 
                        // location.href = 'login';
                    }
                }
            });
        }

        //验证码登录
        function yzmLogin() {
            var mobileNo = $("#loginNo").val();
            var mobilecheckNo = $("#mobilecheckNo").val();
            var serviceName = "userLogin";

            var headerInfo = {},bizInfo = {};
            headerInfo.pageNum = '';
            headerInfo.pageSize = '';
            headerInfo.sessionId = "";

            bizInfo.loginType = "checkNo";
            bizInfo.mobileNo =  mobileNo;
            bizInfo.mobilecheckNo =  mobilecheckNo;
            $.ajax({
                url:'/biz.ajax',
                method:'get',
                cache:false,
                headers: {
                    "Accept" : "text/plain; charset=utf-8",
                    "Content-Type": "text/plain; charset=utf-8"
                },
                data:{
                    'serviceName': serviceName,
                    'headerInfo': JSON.stringify(headerInfo),
                    'bizInfo': JSON.stringify(bizInfo)
                },
                success:function(data,textStatus,jqXHR){
                    console.log("登录返回=",data)
                    var returnData = JSON.parse(data);
                    if(returnData.isSuccess == "T"){
                        //保存数据
                        sessionId = returnData.sessionId;
                        var bodyData = JSON.parse(returnData.body);
                        sessionStorage.setItem("sessionGlobalLoginData",data);

                        //获取用户扩展信息
                        getUserExtData();

                    }else{
                        $(".loginerror").addClass("show").find('span').html(returnData.errorMsg)
                    }
                },
                error:function(xhr,textStatus){
                    console.log('失败=',xhr)
                }
            })
        }

        //获取用户扩展信息
        function getUserExtData(){
            var serviceName = "queryUserExt";
            var headerInfo = {},bizInfo = {};
            headerInfo.pageNum = '';
            headerInfo.pageSize = '';
            headerInfo.sessionId = sessionId;
            console.log("开始登录=",serviceName)
            $.ajax({
                url:'/biz.ajax',
                method:'get',
                cache:false,
                headers: {
                    "Accept" : "text/plain; charset=utf-8",
                    "Content-Type": "text/plain; charset=utf-8"
                },
                data:{
                    'serviceName': serviceName,
                    'headerInfo': JSON.stringify(headerInfo),
                    'bizInfo': JSON.stringify(bizInfo)
                },
                success:function(data,textStatus,jqXHR){
                    console.log("扩展信息返回=",data)
                    var returnData = JSON.parse(data);
                    if(returnData.isSuccess == "T"){
                        //数据存放在session当中
                        sessionStorage.setItem("loginUserExtData",returnData.body);

                        //跳转到个人中心页面
                        window.location.href = "./mine.html"

                    }else{

                    }
                },
                error:function(xhr,textStatus){
                    console.log('失败=',xhr)
                }
            })
        }
    })
})(window, $, jQuery)
